﻿using System;
using System.Linq;
using NUnit.Framework;
using Yala.UnitTests.IocSetup;
using Yala.UnitTests.UnitTestBase;
using Yala.Models.Items;

namespace Yala.UnitTests.Repositories {
    [TestFixture]
    public class ItemsRepository : DbUnitTestBase {
        private const string ITEM_ID = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4";

        IItemsRepository _repo;
        public ItemsRepository() {
            Bootstrapper.ConfigureStructureMap();
        }

        [TestFixtureSetUp]
        public void Init() {
            DatabaseFixtureSetUp();
        }

        [SetUp]
        public void SetUp() {
            DatabaseSetUp();
            _repo = StructureMap.ObjectFactory.GetInstance<IItemsRepository>();
        }

        [TearDown]
        public void TearDown() {
            DatabaseTearDown();
            _repo.Dispose();
        }

        [TestFixtureTearDown]
        public void CleanUp() {
            DatabaseFixtureTearDown();
        }

        [Test]
        public void Can_Get_All_Items() {
            var items = _repo.All();
            Assert.IsNotNull(items);
            Assert.AreEqual(3, items.Count());
        }

        [Test]
        public void Can_Get_Items_By_Id() {
            var itemId = Guid.Parse(ITEM_ID);
            var item = _repo.Single(x => x.Id == itemId);

            Assert.IsNotNull(item);
            Assert.AreEqual(ITEM_ID, item.Id.ToString());
        }

        [Test]
        public void Can_Get_Item_Owner() {
            var itemId = Guid.Parse(ITEM_ID);
            var item = _repo.Single(x => x.Id == itemId);

            Assert.IsNotNull(item.Owner);
            Assert.AreEqual(Guid.Parse("825cb802-193e-4627-9da3-86ef9acb1205"), item.Owner.Id);
        }

        [Test]
        public void Can_Update_Item() {
            var itemId = Guid.Parse(ITEM_ID);
            var item = _repo.Single(x => x.Id == itemId);

            // Update info
            var newName = "UpdatedItemName_1";
            item.Name = newName;
            _repo.Update(item);

            // Get updated item and check to make sure it is updated
            var updatedItem = _repo.Single(x => x.Id ==itemId);

            Assert.IsNotNull(updatedItem);
            Assert.AreEqual(newName, updatedItem.Name);
        }

        [Test]
        public void Can_Get_Items_Owner_AuthProvider() {
            // Select item where Id match the specified Id
            var itemId = Guid.Parse(ITEM_ID);
            var item = _repo.Single(x => x.Id == itemId);

            Assert.IsNotNull(item.Owner.AuthProvider);
            Assert.AreEqual(Guid.Parse("485ac802-993e-4627-9da3-86ef9ecb1295"), item.Owner.AuthProvider.Id);
        }

        [Test]
        public void Can_Delete_Item_By_Id() {
            // Delete item given it's Id
            var itemId = Guid.Parse(ITEM_ID);
            _repo.Delete(x => x.Id == itemId);
            _repo.CommitChanges();

            // Now get all providers
            var items = _repo.All();

            Assert.IsNotNull(items);
            Assert.AreEqual(2, items.Count());
        }

        [Test]
        public void Can_Delete_Item_By_Item() {
            // Delete item given it's Id
            var itemId = Guid.Parse(ITEM_ID);
            var item = _repo.Single(x => x.Id == itemId);
            _repo.Delete(item);
            _repo.CommitChanges();

            // Now get all providers
            var Items = _repo.All();
            Assert.IsNotNull(Items);
            Assert.AreEqual(2, Items.Count());
        }
    }
}
